package com.bytedance.danmaku.render.engine.demo.danmu.project

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.Log
import com.bytedance.danmaku.render.engine.control.DanmakuConfig
import com.bytedance.danmaku.render.engine.data.DanmakuData
import com.bytedance.danmaku.render.engine.demo.danmu.bordertext.GradientDanmaku
import com.bytedance.danmaku.render.engine.demo.danmu.bordertext.GradientDanmakuDrawItem
import com.bytedance.danmaku.render.engine.demo.utils.DRAW_TYPE_GRADIENT_CHANGE_DIGG
import com.bytedance.danmaku.render.engine.demo.utils.DensityUtils.dp2px
import com.bytedance.danmaku.render.engine.demo.utils.dp
import com.bytedance.danmaku.render.engine.render.draw.DrawItem
import com.bytedance.danmaku.render.engine.render.draw.IDrawItemFactory
import com.bytedance.danmaku.render.engine.render.draw.text.TextData
import com.bytedance.danmaku.render.engine.render.draw.text.TextDrawItem
import kotlin.math.max

/**
 * @Description:渐变色可设置border显示隐藏digg图文混排
 * @Author yanxin
 * @Date:  2025/9/8
 */

class GradientChangeDiggDanmaku : DanmakuData() {
    var mTextData: TextData? = null
    var mGradientDanmaku: GradientDanmaku? = null
    var mDiggEmptyTextDanmaku: DiggEmptyTextDanmaku? = null
    var useGradient: Boolean = false
    override var drawType: Int = DRAW_TYPE_GRADIENT_CHANGE_DIGG
}

class GradientChangeDiggDanmakuDrawItem : DrawItem<GradientChangeDiggDanmaku>() {
    private val mTextDanmakuDrawItem = TextDrawItem()
    private val mGradientDanmakuDrawItem = GradientDanmakuDrawItem()
    private val mDiggEmptyTextDrawItem = DiggEmptyTextDrawItem()
    private var mPaddingLeft =dp2px(4F)  //左间距
    private var mPaddingRight = 8.dp //右间距
    private var isUseGradient=false
    override var x: Float = 0F
        set(value) {
            field = value
            updateXY()
        }
    override var y: Float = 0F
        set(value) {
            field = value
            updateXY()
        }

    private fun updateXY() {
        if(isUseGradient)
        {
            mGradientDanmakuDrawItem.x=x+mPaddingLeft
            mGradientDanmakuDrawItem.y=y
            mDiggEmptyTextDrawItem.x=x+mGradientDanmakuDrawItem.width
            mDiggEmptyTextDrawItem.y=y
            Log.e("gradientParent ","===${y}===${height}")
        }
        else
        {
            mTextDanmakuDrawItem.x=x+mPaddingLeft
            mTextDanmakuDrawItem.y=y
            mDiggEmptyTextDrawItem.x=x+mTextDanmakuDrawItem.width
            mDiggEmptyTextDrawItem.y=y
            Log.e("gradientParent ","===${y}===${height}")

        }
    }

    override fun getDrawType(): Int {
        return DRAW_TYPE_GRADIENT_CHANGE_DIGG
    }

    override fun onMeasure(config: DanmakuConfig) {
        mDiggEmptyTextDrawItem.measure(config)
        if(isUseGradient)
        {
            mGradientDanmakuDrawItem.measure(config)
            width = mGradientDanmakuDrawItem.width + mDiggEmptyTextDrawItem.width
            height = max(mGradientDanmakuDrawItem.height, mDiggEmptyTextDrawItem.height)
        }
        else{
            mTextDanmakuDrawItem.measure(config)
            width = mTextDanmakuDrawItem.width + mDiggEmptyTextDrawItem.width
            height = max(mTextDanmakuDrawItem.height, mDiggEmptyTextDrawItem.height)
        }
        updateXY()
    }

    override fun onDraw(canvas: Canvas, config: DanmakuConfig) {
        drawDiggBg(canvas,config)
        if(isUseGradient)
        {
            mGradientDanmakuDrawItem.draw(canvas,config)
        }
        else{
            mTextDanmakuDrawItem.draw(canvas,config)
        }
        mDiggEmptyTextDrawItem.draw(canvas,config)
    }
    private fun drawDiggBg(canvas: Canvas,config: DanmakuConfig) {
        val mBorderBgRectF = RectF()
        val mBorderBgPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
        mBorderBgPaint.setColor(Color.TRANSPARENT)      // 设置边框颜色
        canvas.drawRoundRect(mBorderBgRectF, height / 2, height / 2, mBorderBgPaint)
    }
    override fun onBindData(data: GradientChangeDiggDanmaku) {
        isUseGradient=data.useGradient
        data.mTextData?.let {
            mTextDanmakuDrawItem.bindData(it)
        }
        data.mGradientDanmaku?.let {
            mGradientDanmakuDrawItem.bindData(it)
        }
        data.mDiggEmptyTextDanmaku?.let {
            mDiggEmptyTextDrawItem.bindData(it)
        }
    }

    override fun recycle() {
        super.recycle()
        mTextDanmakuDrawItem.recycle()
        mDiggEmptyTextDrawItem.recycle()
        mDiggEmptyTextDrawItem.recycle()
    }
}

class GradientChangeDiggDanmakuFactory : IDrawItemFactory {

    override fun getDrawType(): Int {
        return DRAW_TYPE_GRADIENT_CHANGE_DIGG
    }

    override fun generateDrawItem(): DrawItem<out DanmakuData> {
        return GradientChangeDiggDanmakuDrawItem()
    }
}
